package logic

import (
	"automated-modeling/automatedrpc/automatedrpc"
	"automated-modeling/automatedrpc/internal/svc"
	"automated-modeling/models/mysql"
	"automated-modeling/pkg"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRegisterLogic {
	return &UserRegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserRegisterLogic) UserRegister(in *automatedrpc.UserRegisterRequest) (*automatedrpc.UserRegisterResponse, error) {
	// todo: add your logic here and delete this line

	var fing mysql.Users
	mysql.DB.Where("mobile = ?", in.Mobile).Find(&fing)
	if fing.ID != 0 {
		return nil, fmt.Errorf("手机号已被使用，不能重复注册")
	}
	var user mysql.Users
	user = mysql.Users{
		Mobile:   in.Mobile,
		Password: pkg.Md5(in.Password),
	}
	err := mysql.DB.Debug().Create(&user).Error
	if err != nil {
		return nil, fmt.Errorf("注册失败")
	}

	return &automatedrpc.UserRegisterResponse{Id: int64(user.ID)}, nil
}
